package com.astrocsr.action.bill;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.astrocsr.action.BaseAction;
import com.astrocsr.business.SecurityListManager;
import com.astrocsr.business.StatementManager;
import com.astrocsr.business.UserManager;
import com.astrocsr.common.Constants;
import com.astrocsr.common.helper.ConfigHelper;
import com.astrocsr.common.helper.ContextHelper;
import com.astrocsr.common.util.DateUtil;
import com.astrocsr.domain.model.ReportDownloadModel;
import com.astrocsr.domain.web.BillInfo;
import com.astrocsr.domain.web.GroupAccount;
import com.astrocsr.domain.web.GroupInfo;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author Created Jan 08, 2014
 */
@Controller("billDownloadAction")
@Scope("prototype")
public class BillDownloadAction extends BaseAction implements
		ModelDriven<ReportDownloadModel> {

	private static final long serialVersionUID = 1L;
	private ReportDownloadModel model = new ReportDownloadModel();

	public ReportDownloadModel getModel() {
		return model;
	}

	@Autowired
	private UserManager userManager;
	@Autowired
	private StatementManager statementManager;
	@Autowired
	private SecurityListManager securityManager;

	static final Logger log = Logger.getLogger(BillDownloadAction.class);

	
	/**
	 * List Groups
	 */
	public String initGroup() throws Exception {
		try {

			// check group
			boolean b = securityManager.checkLastGroup(ContextHelper.getCurrentRole());
			if (b) {
				return "lastgroup";
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return SUCCESS;
	}
	
	/**
	 * Initial Data for reportDownload
	 */
	public String downloadBill() throws Exception {
		try {
			// Build latest 3 months
			int numOfMonths = Integer.valueOf(ConfigHelper.getInstance().get(Constants.REPORT_NUM_OF_MONTHS));
			model.setItemList(userManager.buildMonths(numOfMonths));
			GroupInfo groupInfo = model.getGroupInfo(); 
			if (groupInfo != null && StringUtils.isNotBlank(groupInfo.getGroupCode())) {
				groupInfo = securityManager.getGroup(groupInfo.getGroupCode());
			} else {
				groupInfo = securityManager.getGroup(ContextHelper.getCurrentRole());
			}
			model.setGroupInfo(groupInfo);
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return SUCCESS;
	}
	
	/**
	 * Search Statement Date in Special Month
	 */
	public String executeSearchStatementDateInMonth() throws Exception {
		try {
			String selectedMonth = model.getSelectedMonth();
			GroupInfo groupInfo    = model.getGroupInfo();
			if (StringUtils.isNotBlank(selectedMonth) && selectedMonth.split("-").length ==2
					&& groupInfo != null && StringUtils.isNotBlank(groupInfo.getGroupCode())) {
				String[] smArr = selectedMonth.split("-");
				//String[] accArr = {"086332626-2", "081843909-9", "085397736-7"};
				System.out.println("GroupCode = " + groupInfo.getGroupCode());

				List<GroupAccount> groupAccountList = statementManager.listGroupAccountByGroupCode(groupInfo.getGroupCode(), null, null);

				Date date = DateUtil.createDateLocal(1, Integer.valueOf(smArr[0]), Integer.valueOf(smArr[1]));
				List<BillInfo> billInfoList = statementManager.searchStatementDateInMonth(groupAccountList, date);
				
				if (billInfoList != null && billInfoList.size() > 0) {
					for (BillInfo billInfo : billInfoList) {
						billInfo.setGroupCode(groupInfo.getGroupCode());
					}
				}
				
				model.setBillInfoList(billInfoList);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
}